package com.chw.admin.menu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.chw.annotation.PermissionSecurity;
import com.chw.common.ServiceData;

/**
 * spring mvc控制类
 * 
 * @author Admin
 *
 */
@Controller
@RequestMapping("/sysmenu")
// @Api(value = "/sysmenu")
public class SysMenuController {
	// 日志对象
	private static final Logger log = Logger.getLogger(SysMenuController.class);

	// 服务对象，SPRING自动装配
	@Autowired
	private ISysMenuService sysMenuService;


	/**
	 * 根据菜单ID获取菜单对象
	 * 
	 * @param request
	 * @param id
	 *            菜单ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSysMenuById.do", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "getSysMenuById", httpMethod = "GET", response =
	// ServiceData.class, notes = "根据菜单ID获取菜单对象")
	// @ApiParam(value = "id", required = true) @PathVariable("id")
	@PermissionSecurity(name="查询",module="菜单管理",btnfunc="select_menu")
	public ServiceData get(HttpServletRequest request, String id) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		SysMenu sysMenu = sysMenuService.selectById(id);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(sysMenu);
		return ret;
	}

	/**
	 * 根据查询条件获取菜单对象，移除接口控制权限
	 * 
	 * @param request
	 * @param sysMenu
	 *            菜单对象
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findSysMenu.notPermSc", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "findSysMenu", httpMethod = "GET", response =
	// ServiceData.class, notes = "根据查询条件获取菜单对象")
	// @RequestBody
	public ServiceData find(HttpServletRequest request, SysMenu sysMenu) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		List<SysMenu> sysMenus = sysMenuService.find(sysMenu);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(sysMenus);
		return ret;
	}

	/**
	 * 判断是否有子菜单
	 * 
	 * @param request
	 * @param id
	 *            菜单ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hasSubSysMenu.do", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "hasSubSysMenu", httpMethod = "GET", response =
	// ServiceData.class, notes = "判断是否有子菜单")
	// @ApiParam(value = "id", required = true)
	@PermissionSecurity(name="查询",module="菜单管理",btnfunc="select_menu")
	public ServiceData hasSubMenu(HttpServletRequest request, String id) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		boolean flag = sysMenuService.hasSub(id);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(flag);
		return ret;
	}

	/**
	 * 分页查询菜单
	 * 
	 * @param request
	 * @param sysMenu
	 *            菜单对象
	 * @param currentPage
	 *            当前页码
	 * @param pageSize
	 *            每页大小
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageSysMenu.do", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "pageSysMenu", httpMethod = "POST", response =
	// ServiceData.class, notes = "分页查询菜单")
	// @RequestBody SysMenu sysMenu,
	// @ApiParam(value = "pageSize", required = true)
	// @ApiParam(value = "currentPage", required = true)
	@PermissionSecurity(name="查询",module="菜单管理",btnfunc="select_menu")
	public ServiceData page(HttpServletRequest request, SysMenu sysMenu, Integer currentPage, Integer pageSize)
			throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		PageInfo sysMenus = sysMenuService.page(sysMenu, currentPage, pageSize);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(sysMenus);
		return ret;
	}

	/**
	 * 更新菜单状态
	 * 
	 * @param request
	 * @param id
	 *            菜单ID
	 * @param status
	 *            菜单状态
	 * @throws Exception
	 */
	@RequestMapping(value = "/updateStatusSysMenu.do", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "updateStatusSysMenu", httpMethod = "POST", notes =
	// "更新菜单状态")
	// @ApiParam(value = "id", required = true)
	// @ApiParam(value = "status", required = true)
	@PermissionSecurity(name="启用禁用",module="菜单管理",btnfunc="update_menu_status")
	public void updateStatus(HttpServletRequest request, String id, String status) throws Exception {
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		sysMenuService.updateStatus(id, status);
	}

	/**
	 * 删除菜单
	 * 
	 * @param request
	 * @param id
	 *            菜单ID
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteSysMenu.do", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "deleteSysMenu", httpMethod = "POST", response =
	// ServiceData.class, notes = "删除菜单")
	// @ApiParam(value = "id", required = true)
	@PermissionSecurity(name="删除",module="菜单管理",btnfunc="delete_menu")
	public ServiceData delete(HttpServletRequest request, String id) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		sysMenuService.delete(id);
		ret.setCode(request, ServiceData.RetCode.Success);
		return ret;
	}

	/**
	 * 保存菜单
	 * 
	 * @param request
	 * @param sysMenu
	 *            菜单对象
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/saveSysMenu.do", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "saveOrUpdateSysMenu", httpMethod = "POST",
	// response = ServiceData.class, notes = "保存菜单")
	// @RequestBody
	@PermissionSecurity(name="新增",module="菜单管理",btnfunc="add_menu")
	public ServiceData save(HttpServletRequest request, SysMenu sysMenu) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		sysMenuService.saveOrUpdate(sysMenu);
		ret.setCode(request, ServiceData.RetCode.Success);
		return ret;
	}
	
	@RequestMapping(value = "/updateSysMenu.do", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	// @ApiOperation(value = "saveOrUpdateSysMenu", httpMethod = "POST",
	// response = ServiceData.class, notes = "保存菜单")
	// @RequestBody
	@PermissionSecurity(name="修改",module="菜单管理",btnfunc="update_menu")
	public ServiceData update(HttpServletRequest request, SysMenu sysMenu) throws Exception {
		// 返回统一的服务端数据
		ServiceData ret = new ServiceData();
		// 业务操作可以不捕获异常,由统一的异常处理方法处理
		sysMenuService.saveOrUpdate(sysMenu);
		ret.setCode(request, ServiceData.RetCode.Success);
		return ret;
	}
	
	@RequestMapping(value = "/hasMenuNameSysMenu.do", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@PermissionSecurity(name="查询",module="菜单管理",btnfunc="select_menu")
	public ServiceData hasMenuName(HttpServletRequest request, String name,String id) throws Exception {
		ServiceData ret = new ServiceData();
		boolean flag = sysMenuService.hasMenuName(name,id);
		ret.setCode(request, ServiceData.RetCode.Success);
		ret.setBo(flag);
		return ret;
	}
}